﻿Imports JPC.Medialibrary.DAL
Imports JPC.Medialibrary.BLL
Imports JPC.Medialibrary.UI
Imports System.Web.Services
Imports AjaxControlToolkit

Partial Class Music
    Inherits System.Web.UI.Page

    'Private _artistId As Integer = -1
    'Private _tagId As Integer = -1
    Private _noResults As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        System.Threading.Thread.Sleep(80)

        If Page.IsPostBack Then
            Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)
            If Me.cmdName.Value <> "" Then
                ProcesarComando(Me.btnText.Value, Me.cmdName.Value, CInt(Me.cmdArg.Value))
                Me.cmdName.Value = ""
                Me.cmdArg.Value = ""
                Me.btnText.Value = ""
            End If
        Else
            MySession.BreadCrumbList = Nothing
            MySession.LastBreadcrumb = Nothing
            Breadcrumb.AddItem("Inicio", "Inicio", 0)
            Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)
        End If

        If MySession.currentPlaylistSongs.Count = 0 Then
            Me.btnCopySelection.Attributes.CssStyle("display") = "none"
            Me.btnDeleteSelection.Attributes.CssStyle("display") = "none"
            Me.btnSaveSelection.Attributes.CssStyle("display") = "none"
        End If

    End Sub

    Protected Sub btnSongs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSongs.Click
        ProcesarComando(btnSongs.Text, "GetSongsByText", -1)
    End Sub

    Protected Sub btnArtists_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnArtists.Click
        ProcesarComando(btnArtists.Text, "GetArtistsByText", -1)
    End Sub

    Protected Sub btnAlbums_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlbums.Click
        ProcesarComando(btnAlbums.Text, "GetAlbumsByText", -1)
    End Sub

    Protected Sub btnPlaylists_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlaylists.Click
        ProcesarComando(btnPlaylists.Text, "GetPlaylistsByText", -1)
    End Sub

    Protected Sub btnGenres_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenres.Click
        ProcesarComando(btnGenres.Text, "GetTagsByText", -1)
    End Sub

    Protected Sub btnStatistics_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStatistics.Click
        ProcesarComando(btnStatistics.Text, "GetStatisticsMenu", -1)
    End Sub

    Protected Sub btnSelected_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelected.Click
        ProcesarComando(btnSelected.Text, "GetSelectedSongs", -1)
    End Sub

    Protected Sub btnDefault_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDefault.Click
        ProcesarComando(btnSongs.Text, "GetSongsByText", -1)
    End Sub

    Private Sub GetSongsByAlbum(ByVal albumId As Integer)
        SelectMenu(btnSongs)
        Dim _songs As List(Of Song)
        _songs = MedialibraryManager.GetSongsByAlbum(albumId)
        If _songs.Count = 0 Then
            Dim _song As New Song
            _song.Title = "La búsqueda no produjo resultados"
            _songs.Add(_song)
            _noResults = True
        End If
        lstMain.DataSource = _songs
        lstMain.DataBind()
    End Sub

    Private Sub GetSongsByArtist(ByVal artistId As Integer)
        SelectMenu(btnSongs)
        Dim _songs As List(Of Song)
        _songs = MedialibraryManager.GetSongsByArtist(artistId)
        If _songs.Count = 0 Then
            Dim _song As New Song
            _song.Title = "La búsqueda no produjo resultados"
            _songs.Add(_song)
            _noResults = True
        End If
        lstMain.DataSource = _songs
        lstMain.DataBind()
    End Sub

    Private Sub GetSongsByAlbumArtist(ByVal albumId As Integer, ByVal artistId As Integer)
        SelectMenu(btnSongs)
        Dim _songs As List(Of Song)
        _songs = MedialibraryManager.GetSongsByAlbumArtist(albumId, artistId)
        If _songs.Count = 0 Then
            Dim _song As New Song
            _song.Title = "La búsqueda no produjo resultados"
            _songs.Add(_song)
            _noResults = True
        End If
        lstMain.DataSource = _songs
        lstMain.DataBind()
    End Sub

    Private Sub GetSongsByAlbumTag(ByVal albumId As Integer, ByVal tagId As Integer)
        SelectMenu(btnSongs)
        Dim _songs As List(Of Song)
        _songs = MedialibraryManager.GetSongsByAlbumTag(albumId, tagId)
        If _songs.Count = 0 Then
            Dim _song As New Song
            _song.Title = "La búsqueda no produjo resultados"
            _songs.Add(_song)
            _noResults = True
        End If
        lstMain.DataSource = _songs
        lstMain.DataBind()
    End Sub

    Private Sub GetSongsByPlaylist(ByVal playlistId As Integer)
        SelectMenu(btnSongs)
        Dim _songs As List(Of Song)
        _songs = MedialibraryManager.GetSongsByPlaylist(playlistId)
        If _songs.Count = 0 Then
            Dim _song As New Song
            _song.Title = "La búsqueda no produjo resultados"
            _songs.Add(_song)
            _noResults = True
        End If
        lstMain.DataSource = _songs
        lstMain.DataBind()
    End Sub

    Private Sub GetSongsBytag(ByVal tagId As Integer)
        SelectMenu(btnSongs)
        Dim _songs As List(Of Song)
        _songs = MedialibraryManager.GetSongsByTag(tagId)
        If _songs.Count = 0 Then
            Dim _song As New Song
            _song.Title = "La búsqueda no produjo resultados"
            _songs.Add(_song)
            _noResults = True
        End If
        lstMain.DataSource = _songs
        lstMain.DataBind()
    End Sub

    Private Sub GetSongsByText()
        SelectMenu(btnSongs)
        If txtSearch.Text <> "" Then
            Dim _songs As List(Of Song)
            _songs = MedialibraryManager.GetSongsByText(txtSearch.Text.Trim)
            If _songs.Count = 0 Then
                Dim _song As New Song
                _song.Title = "La búsqueda no produjo resultados"
                _songs.Add(_song)
                _noResults = True
            End If
            lstMain.DataSource = _songs
        Else
            lstMain.DataSource = New AbcMenu("Songs")
        End If
        lstMain.DataBind()
    End Sub

    Private Sub GetSongsByInitial(ByVal initial As Integer)
        SelectMenu(btnSongs)
        Dim _songs As List(Of Song)
        _songs = MedialibraryManager.GetSongsByInitial(initial)
        If _songs.Count = 0 Then
            Dim _song As New Song
            _song.Title = "La búsqueda no produjo resultados"
            _songs.Add(_song)
            _noResults = True
        End If
        lstMain.DataSource = _songs
        lstMain.DataBind()
    End Sub

    Private Sub GetArtistsByText()
        SelectMenu(btnArtists)
        If txtSearch.Text <> "" Then
            Dim _artists As List(Of Artist)
            _artists = MedialibraryManager.GetArtistsByText(txtSearch.Text.Trim)
            If _artists.Count = 0 Then
                Dim _artist As New Artist
                _artist.ArtistName = "La búsqueda no produjo resultados"
                _artists.Add(_artist)
                _noResults = True
            End If
            lstMain.DataSource = _artists
        Else
            lstMain.DataSource = New AbcMenu("Artists")
        End If
        lstMain.DataBind()
    End Sub

    Private Sub GetArtistsByTag(ByVal tagId As Integer)
        SelectMenu(btnArtists)
        Dim _artists As List(Of Artist)
        _artists = MedialibraryManager.GetArtistsByTag(tagId)
        If _artists.Count = 0 Then
            Dim _artist As New Artist
            _artist.ArtistName = "La búsqueda no produjo resultados"
            _artists.Add(_artist)
            _noResults = True
        End If
        lstMain.DataSource = _artists
        lstMain.DataBind()
    End Sub

    Private Sub GetArtistsByInitial(ByVal initial As Integer)
        SelectMenu(btnArtists)
        Dim _artists As List(Of Artist)
        _artists = MedialibraryManager.GetArtistsByInitial(initial)
        If _artists.Count = 0 Then
            Dim _artist As New Artist
            _artist.ArtistName = "La búsqueda no produjo resultados"
            _artists.Add(_artist)
            _noResults = True
        End If
        lstMain.DataSource = _artists
        lstMain.DataBind()
    End Sub


    Private Sub GetAlbumsByText()
        SelectMenu(btnAlbums)
        If txtSearch.Text <> "" Then
            Dim _albums As List(Of Album)
            _albums = MedialibraryManager.GetAlbumsByText(txtSearch.Text.Trim)
            If _albums.Count = 0 Then
                Dim _album As New Album
                _album.AlbumName = "La búsqueda no produjo resultados"
                _albums.Add(_album)
                _noResults = True
            End If
            lstMain.DataSource = _albums
        Else
            lstMain.DataSource = New AbcMenu("Albums")
        End If
        lstMain.DataBind()
    End Sub

    Private Sub GetStatisticsMenu()
        SelectMenu(btnStatistics)
        lstMain.DataSource = New StatisticsMenu()
        lstMain.DataBind()
    End Sub

    Private Sub GetAlbumsByArtist(ByVal artistId As Integer)
        SelectMenu(btnAlbums)
        Dim _albums As List(Of Album)
        _albums = MedialibraryManager.GetAlbumsByArtist(artistId)
        If _albums.Count <= 1 Then
            GetSongsByArtist(artistId)
        Else
            Dim _album As New Album
            _album.AlbumName = "Todas las canciones"
            _album.AlbumId = -1
            _album.ArtistId = artistId
            _albums.Insert(0, _album)
            lstMain.DataSource = _albums
            lstMain.DataBind()
        End If
    End Sub

    Private Sub GetAlbumsByTag(ByVal tagId As Integer)
        SelectMenu(btnAlbums)
        Dim _albums As List(Of Album)
        _albums = MedialibraryManager.GetAlbumsByTag(tagId)
        If _albums.Count = 0 Then
            Dim _album As New Album
            _album.AlbumName = "La búsqueda no produjo resultados"
            _albums.Add(_album)
            _noResults = True
        End If
        lstMain.DataSource = _albums
        lstMain.DataBind()
    End Sub

    Private Sub GetAlbumsByInitial(ByVal initial As Integer)
        SelectMenu(btnAlbums)
        Dim _albums As List(Of Album)
        _albums = MedialibraryManager.GetAlbumsByInitial(initial)
        If _albums.Count = 0 Then
            Dim _album As New Album
            _album.AlbumName = "La búsqueda no produjo resultados"
            _albums.Add(_album)
            _noResults = True
        End If
        lstMain.DataSource = _albums
        lstMain.DataBind()
    End Sub

    Private Sub GetPlayListsByText()
        SelectMenu(btnPlaylists)
        If txtSearch.Text <> "" Then
            Dim _playLists As List(Of Playlist)
            _playLists = MedialibraryManager.GetPlayListsByText(txtSearch.Text.Trim)
            If _playLists.Count = 0 Then
                Dim _playList As New Playlist
                _playList.PlayListName = "La búsqueda no produjo resultados"
                _playLists.Add(_playList)
                _noResults = True
            End If
            lstMain.DataSource = _playLists
        Else
            lstMain.DataSource = MedialibraryManager.GetAllPlaylists
        End If
        lstMain.DataBind()
    End Sub

    Private Sub GetTagsByText()
        SelectMenu(btnGenres)
        If txtSearch.Text <> "" Then
            Dim _tags As List(Of Tag)
            _tags = MedialibraryManager.GetTagsByText(txtSearch.Text.Trim)
            If _tags.Count = 0 Then
                Dim _tag As New Tag
                _tag.TagName = "La búsqueda no produjo resultados"
                _tags.Add(_tag)
                _noResults = True
            End If
            lstMain.DataSource = _tags
        Else
            'lstMain.DataSource = MedialibraryManager.GetAllTags
            lstMain.DataSource = New AbcMenu("Tags")
        End If
        lstMain.DataBind()
    End Sub

    Private Sub GetTagsByInitial(ByVal initial As Integer)
        SelectMenu(btnGenres)
        Dim _tags As List(Of Tag)
        _tags = MedialibraryManager.GetTagsByInitial(initial)
        If _tags.Count = 0 Then
            Dim _tag As New Tag
            _tag.TagName = "La búsqueda no produjo resultados"
            _tags.Add(_tag)
            _noResults = True
        End If

        lstMain.DataSource = _tags
        lstMain.DataBind()
    End Sub

    Private Sub GetSelectedSongs()
        SelectMenu(btnSelected)
        If MySession.currentPlaylistSongs.Count = 0 Then
            Dim _songs As New List(Of Song)
            Dim _song As New Song
            _song.Title = "No hay canciones seleccionadas"
            _songs.Add(_song)
            _noResults = True
            lstMain.DataSource = _songs
        Else
            lstMain.DataSource = MySession.currentPlaylistSongs
        End If
        lstMain.DataBind()
    End Sub

    Private Sub GetTagsMenu(ByVal tagId As Integer)
        lstMain.DataSource = New MusicMenu("Tag", tagId)
        lstMain.DataBind()
    End Sub

    Private Sub SelectMenu(ByVal btn As LinkButton)
        btnAlbums.CssClass = ""
        btnArtists.CssClass = ""
        btnGenres.CssClass = ""
        btnPlaylists.CssClass = ""
        btnSongs.CssClass = ""
        btnSelected.CssClass = ""
        btnStatistics.CssClass = ""
        btn.CssClass = "selected"
        MySession.CurrentMenu = btn.CommandName
        Me.currentMenu.Value = MySession.CurrentMenu
    End Sub

    Protected Sub lstBreadcrumb_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lstBreadcrumb.ItemCommand

        Dim index As Integer = CInt(e.CommandArgument)

        Dim li As ListItem = MySession.BreadCrumbList(index)
        Breadcrumb.RemoveRange(index)
        ProcesarComando(li.Text, li.CommandName, CInt(li.CommandArgument))

    End Sub

    Private Sub ProcesarComando(ByVal Text As String, ByVal CommandName As String, ByVal CommandArgument As Integer)

        If Right(CommandName, 7) = "ByAlbum" Then
            Dim _album As New Album(CommandArgument)
            Text = _album.AlbumNameProper
        End If

        Breadcrumb.AddItem(Text, CommandName, CommandArgument)
        Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)

        Select Case CommandName
            Case "Inicio"
                Response.Redirect("~/Start.aspx")
            Case "GetAlbumsByArtist"
                GetAlbumsByArtist(CommandArgument)
            Case "GetAlbumsByTag"
                GetAlbumsByTag(CommandArgument)
            Case "GetAlbumsByText"
                GetAlbumsByText()
            Case "GetAlbumsByInitial"
                GetAlbumsByInitial(CommandArgument)
            Case "GetArtistsByTag"
                GetArtistsByTag(CommandArgument)
            Case "GetArtistsByText"
                GetArtistsByText()
            Case "GetArtistsByInitial"
                GetArtistsByInitial(CommandArgument)
            Case "GetSongsByAlbum"
                If MySession.LastBreadcrumb.ObjectType = "Artist" And CInt(MySession.LastBreadcrumb.CommandArgument) <> -1 Then
                    GetSongsByAlbumArtist(CommandArgument, CInt(MySession.LastBreadcrumb.CommandArgument))
                ElseIf MySession.LastBreadcrumb.ObjectType = "Tag" And CInt(MySession.LastBreadcrumb.CommandArgument) <> -1 Then
                    GetSongsByAlbumTag(CommandArgument, CInt(MySession.LastBreadcrumb.CommandArgument))
                Else
                    GetSongsByAlbum(CommandArgument)
                End If
            Case "GetSongsByArtist"
                GetSongsByArtist(CommandArgument)
            Case "GetSongsByPlaylist"
                GetSongsByPlaylist(CommandArgument)
            Case "GetSongsByTag"
                GetSongsBytag(CommandArgument)
            Case "GetSongsByText"
                GetSongsByText()
            Case "GetSongsByInitial"
                GetSongsByInitial(CommandArgument)
            Case "GetTagsMenu"
                GetTagsMenu(CommandArgument)
            Case "GetTagsByText"
                GetTagsByText()
            Case "GetTagsByInitial"
                GetTagsByInitial(CommandArgument)
            Case "GetPlaylistsByText"
                GetPlayListsByText()
            Case "GetSelectedSongs"
                GetSelectedSongs()
            Case "GetStatisticsMenu"
                GetStatisticsMenu()
            Case "ShowEditTag"
                ShowEditTag(CommandArgument)
            Case "ModifyTag"
                ModifyTag(CommandArgument)
            Case "ShowEditArtist"
                ShowEditArtist(CommandArgument)
            Case "ModifyArtist"
                ModifyArtist(CommandArgument)
            Case "ShowEditSong"
                ShowEditSong(CommandArgument)
            Case "ModifySong"
                ModifySong(CommandArgument)
        End Select

    End Sub

    Protected Sub ShowEditTag(ByVal tagId As Integer)

        Dim _tag As New Tag(tagId)
        Me.txtEstilo.Text = _tag.TagName.Trim
        btnOkEditTag.OnClientClick = "$('.EditPanel').fadeOut(100);setAction('ModifyTag'," + tagId.ToString + ",'" + btnOkEditTag.ClientID + "');"

        EditTag.Visible = True

    End Sub

    Protected Sub ModifyTag(ByVal tagId As Integer)

        MedialibraryManager.ModifyTag(tagId, txtEstilo.Text.Trim)
        RetrocederMenu(2)

    End Sub

    Protected Sub ShowEditArtist(ByVal artistId As Integer)

        Dim _artist As New Artist(artistId)
        Me.txtArtista.Text = _artist.ArtistNameProper.Trim
        btnOkEditArtist.OnClientClick = "$('.EditPanel').fadeOut(100);setAction('ModifyArtist'," + artistId.ToString + ",'" + btnOkEditArtist.ClientID + "');"

        EditArtist.Visible = True

    End Sub

    Protected Sub ModifyArtist(ByVal artistId As Integer)

        MedialibraryManager.ModifyArtist(artistId, txtArtista.Text.Trim)
        RetrocederMenu(2)

    End Sub

    Protected Sub ShowEditSong(ByVal songId As Integer)

        Dim _song As New Song(songId)
        Me.txtCancion.Text = _song.TitleProper.Trim
        Me.txtAlbum.Text = _song.AlbumNameProper.Trim
        Me.txtArtist.Text = _song.ArtistNameproper.Trim
        'Me.txtTags.Text = _song.Tags.Trim
        btnOkEditSong.OnClientClick = "$('.EditPanel').fadeOut(100);setAction('ModifySong'," + songId.ToString + ",'" + btnOkEditSong.ClientID + "');"

        EditSong.Visible = True

    End Sub

    Protected Sub ModifySong(ByVal songId As Integer)

        MedialibraryManager.ModifySong(songId, txtCancion.Text.Trim, Me.txtAlbum.Text, Me.txtArtist.Text) ', Me.txtTags.Text)
        RetrocederMenu(2)

    End Sub

    Protected Sub lstMain_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lstMain.ItemDataBound
        If (e.Item.ItemType = ListViewItemType.DataItem) Then
            Dim btn As LinkButton = CType(e.Item.FindControl("btnMain"), LinkButton)

            Dim dataItem As Object = CType(e.Item, ListViewDataItem).DataItem

            Dim btnPlus As LinkButton = CType(e.Item.FindControl("btnPlus"), LinkButton)
            Dim btnUp As LinkButton = CType(e.Item.FindControl("btnUp"), LinkButton)
            Dim btnDown As LinkButton = CType(e.Item.FindControl("btnDown"), LinkButton)
            Dim btnDelete As LinkButton = CType(e.Item.FindControl("btnDelete"), LinkButton)
            Dim btnEdit As LinkButton = CType(e.Item.FindControl("btnEdit"), LinkButton)

            Dim pnlRow As Panel = CType(e.Item.FindControl("pnlRow"), Panel)
            Dim pnlPlus As Panel = CType(e.Item.FindControl("pnlAdd"), Panel)
            Dim pnlUp As Panel = CType(e.Item.FindControl("PanelUp"), Panel)
            Dim pnlDown As Panel = CType(e.Item.FindControl("PanelDown"), Panel)
            Dim pnlDelete As Panel = CType(e.Item.FindControl("pnlDelete"), Panel)
            Dim pnlEdit As Panel = CType(e.Item.FindControl("pnlEdit"), Panel)

            pnlPlus.Visible = False
            pnlUp.Visible = False
            pnlDown.Visible = False
            pnlDelete.Visible = False
            pnlEdit.Visible = False

            Dim btnId As String = "'" + btn.ClientID + "'"

            Select Case dataItem.GetType.Name

                Case "Song"
                    Dim _song As Song = CType(dataItem, Song)
                    btn.Text = _song.TitleProper.Trim
                    Dim lastCommand As String = MySession.LastBreadcrumb.CommandName
                    If InStr(lastCommand, "ByArtist") <> 0 Then
                        btn.Text = btn.Text + " <span>en " + _song.AlbumNameProper.Trim + "</span>"
                    ElseIf InStr(lastCommand, "ByAlbum") <> 0 Then
                        btn.Text = btn.Text + " <span>por " + _song.ArtistNameProper.Trim + "</span>"
                    ElseIf _song.SongId <> 0 Then
                        btn.Text = btn.Text + " <span>por " + _song.ArtistNameProper.Trim + " en " + _song.AlbumNameProper.Trim + "</span>"
                    End If
                    If MySession.CurrentMenu = "Selected" Then
                        pnlRow.CssClass = "row songsselected"
                        btn.OnClientClick = "javascript:play_song(" & _song.SongId.ToString & ",true);return false;"
                        pnlUp.Visible = True
                        btnUp.OnClientClick = "javascript:move_up(" & _song.SongId.ToString & ",'" & pnlRow.ClientID & "');return false;"
                        pnlDown.Visible = True
                        btnDown.OnClientClick = "javascript:move_down(" & _song.SongId.ToString & ",'" & pnlRow.ClientID & "');return false;"
                        pnlDelete.Visible = True
                        btnDelete.OnClientClick = "javascript:remove_song(" & _song.SongId.ToString & ",'" & pnlRow.ClientID & "');return false;"
                        If MySession.isPlayingFromPlaylist AndAlso MySession.SelectedSong.SongId = _song.SongId Then
                            pnlRow.CssClass = pnlRow.CssClass + " selected"
                        End If
                    Else
                        pnlRow.CssClass = "row addedit"
                        btn.OnClientClick = "javascript:play_song(" & _song.SongId.ToString & ",false);return false;"
                        btnPlus.OnClientClick = "javascript:add_song_to_current_playlist(" + _song.SongId.ToString + ");return false;"
                        pnlPlus.Visible = True
                        pnlEdit.Visible = True
                        btnEdit.OnClientClick = "javascript:setAction('ShowEditSong'," + _song.SongId.ToString + "," + btnId + ");"
                    End If

                Case "Artist"
                    Dim _artist As Artist = CType(dataItem, Artist)
                    btn.OnClientClick = "javascript:setAction('GetAlbumsByArtist'," + _artist.ArtistId.ToString + "," + btnId + ");"
                    btn.Text = _artist.ArtistNameProper.Trim
                    pnlRow.CssClass = "row edit"
                    pnlEdit.Visible = True
                    btnEdit.OnClientClick = "javascript:setAction('ShowEditArtist'," + _artist.ArtistId.ToString + "," + btnId + ");"

                Case "Album"
                    pnlRow.CssClass = "row add"
                    Dim _album As Album = CType(dataItem, Album)
                    btn.Text = _album.AlbumNameProper.Trim
                    If _album.AlbumId = -1 Then
                        btn.OnClientClick = "javascript:setAction('GetSongsByArtist'," + _album.ArtistId.ToString + "," + btnId + ");"
                        btnPlus.OnClientClick = "javascript:add_artist_to_current_playlist(" + _album.ArtistId.ToString + ");return false;"
                        pnlPlus.Visible = True
                    Else
                        btn.OnClientClick = "javascript:setAction('GetSongsByAlbum'," + _album.AlbumId.ToString + "," + btnId + ");"
                        If MySession.LastBreadcrumb.ObjectType = "Artist" And CInt(MySession.LastBreadcrumb.CommandArgument) <> -1 Then
                            btnPlus.OnClientClick = "javascript:add_album_to_current_playlist(" + _album.AlbumId.ToString + "," + MySession.LastBreadcrumb.CommandArgument + ",-1);return false;"
                        ElseIf MySession.LastBreadcrumb.ObjectType = "Tag" And CInt(MySession.LastBreadcrumb.CommandArgument) <> -1 Then
                            btnPlus.OnClientClick = "javascript:add_album_to_current_playlist(" + _album.AlbumId.ToString + ",-1," + MySession.LastBreadcrumb.CommandArgument + ");return false;"
                        Else
                            btnPlus.OnClientClick = "javascript:add_album_to_current_playlist(" + _album.AlbumId.ToString + ",-1,-1);return false;"
                        End If
                        If MySession.LastBreadcrumb.ObjectType <> "Artist" Then
                            btn.Text = btn.Text & " <span>por " & _album.ArtistName & "</span>"
                        End If
                        pnlPlus.Visible = True
                        End If

                Case "Playlist"
                        pnlRow.CssClass = "row playlist"
                        Dim _playlist As Playlist = CType(dataItem, Playlist)
                        btn.OnClientClick = "javascript:setAction('GetSongsByPlaylist'," + _playlist.PlaylistId.ToString + "," + btnId + ");"
                        btn.Text = _playlist.PlayListName.Trim
                        btnPlus.OnClientClick = "javascript:add_playlist_to_current_playlist(" + _playlist.PlaylistId.ToString + ",'" + _playlist.PlayListName.Trim + "');return false;"
                        pnlPlus.Visible = True
                        btnDelete.OnClientClick = "javascript:confirm_delete_playlist(" + _playlist.PlaylistId.ToString + ",'" & pnlRow.ClientID & "');return false;"
                        pnlDelete.Visible = True

                Case "Tag"
                        Dim _tag As Tag = CType(dataItem, Tag)
                        btn.OnClientClick = "javascript:setAction('GetTagsMenu'," + _tag.TagId.ToString + "," + btnId + ");"
                        btn.Text = _tag.TagName.Trim
                        pnlRow.CssClass = "row edit"
                        pnlEdit.Visible = True
                        btnEdit.OnClientClick = "javascript:setAction('ShowEditTag'," + _tag.TagId.ToString + "," + btnId + ");"

                Case "ListItem"
                        Dim _listItem As ListItem = CType(dataItem, ListItem)
                        btn.OnClientClick = "javascript:setAction('" + _listItem.CommandName.Trim + "'," + _listItem.CommandArgument.Trim + "," + btnId + ");"
                        btn.Text = _listItem.Text.Trim

            End Select
            If _noResults Then
                pnlRow.CssClass = "row"
                btn.OnClientClick = "javascript:return false;"
            End If
        End If

    End Sub

    Protected Sub lstBreadcrumb_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lstBreadcrumb.ItemDataBound

        Breadcrumb.ItemDataBound(sender, e)

    End Sub

    <WebMethod()> _
    Public Shared Function AddSongToCurrentPlaylist(ByVal songId As Integer) As Array
        CurrentPlayListManager.AddSongToCurrentPlaylist(songId)
        Return PlayerStatusChanged()
    End Function

    <WebMethod()> _
    Public Shared Function AddPlaylistToCurrentPlaylist(ByVal playlistId As Integer) As Array
        CurrentPlayListManager.AddPlayListToCurrentPlaylist(playlistId)
        Return PlayerStatusChanged()
    End Function

    <WebMethod()> _
    Public Shared Function AddAlbumToCurrentPlaylist(ByVal albumId As Integer, ByVal artistId As Integer, ByVal tagId As Integer) As Array
        If artistId = -1 And tagId = -1 Then
            CurrentPlayListManager.AddAlbumToCurrentPlaylist(albumId)
        ElseIf tagId = -1 Then
            CurrentPlayListManager.AddAlbumArtistToCurrentPlaylist(albumId, artistId)
        Else
            CurrentPlayListManager.AddAlbumTagToCurrentPlaylist(albumId, tagId)
        End If
        Return PlayerStatusChanged()
    End Function

    <WebMethod()> _
    Public Shared Function AddArtistToCurrentPlaylist(ByVal artistId As Integer) As Array
        CurrentPlayListManager.AddArtistToCurrentPlaylist(artistId)
        Return PlayerStatusChanged()
    End Function

    <WebMethod()> _
    Public Shared Function ClearCurrentPlaylist() As Array
        CurrentPlayListManager.ClearCurrentPlaylist2()
        Return PlayerStatusChanged()
    End Function

    <WebMethod()> _
    Public Shared Function Remove(ByVal songId As Integer) As Array
        CurrentPlayListManager.Remove2(songId)
        Return PlayerStatusChanged()
    End Function

    <WebMethod()> _
    Public Shared Function PlayerStatusChanged() As Array
        Dim aReturn(8) As String

        aReturn(0) = ""                         'Song Title
        aReturn(1) = ""                         'Artist Name
        aReturn(2) = "btnPrev btnPrevDimmed"    'CSS Botón Previous
        aReturn(3) = "return false;"            'onclick Action de Botón Previous
        aReturn(4) = "btnPlay btnPlayDimmed"    'CSS Botón Play
        aReturn(5) = "return false;"            'onclick Action de Botón Play
        aReturn(6) = "btnNext btnNextDimmed"    'CSS Botón Next
        aReturn(7) = "return false;"            'onclick Action de Botón Next
        aReturn(8) = MySession.currentPlaylistSongs.Count

        If MySession.playerStatus = ePlayerStatus.Playing Or MySession.playerStatus = ePlayerStatus.Paused Then
            aReturn(0) = MySession.SelectedSong.TitleProper
            If MySession.SelectedSong.ArtistId <> 0 Then
                aReturn(0) = aReturn(0) & " - " & MySession.SelectedSong.ArtistNameProper
            End If
            'If MySession.SelectedSong.AlbumId <> 0 Then
            'aReturn(0) = aReturn(0) & " - " & MySession.SelectedSong.AlbumNameProper
            'End If
            'aReturn(1) = MySession.SelectedSong.ArtistNameProper

            If MySession.playerStatus = ePlayerStatus.Playing Then
                aReturn(4) = "btnPlay btnPause"
                aReturn(5) = "Pause();return false;"
            Else
                aReturn(4) = "btnPlay"
                aReturn(5) = "Play();return false;"
            End If

            If MySession.isPlayingFromPlaylist And _
               MySession.currentPlaylistSongs.Count > 1 And _
               MySession.CurrentMenu = "Selected" Then
                If MySession.SelectedSong.SongId <> MySession.currentPlaylistSongs(0).SongId Then
                    aReturn(2) = "btnPrev"
                    aReturn(3) = "Previous();return false;"
                End If
                If MySession.SelectedSong.SongId <> MySession.currentPlaylistSongs(MySession.currentPlaylistSongs.Count - 1).SongId Then
                    aReturn(6) = "btnNext"
                    aReturn(7) = "Next();return false;"
                End If
            End If

        Else
            If MySession.currentPlaylistSongs.Count > 0 Then
                aReturn(4) = "btnPlay"    'CSS Botón Play
                aReturn(5) = "play_song_from_playlist('" & MySession.currentPlaylistSongs(0).SongId & "');return false;"            'onclick Action de Botón Play
            End If
        End If

        Return aReturn

    End Function

    <WebMethod()> _
    Public Shared Function Previous() As Integer
        Return CurrentPlayListManager.Previous2()
    End Function

    <WebMethod()> _
    Public Shared Function [Next]() As Integer
        If MySession.isPlayingFromPlaylist Then
            Return CurrentPlayListManager.Next2()
        Else
            Return 0
        End If
    End Function

    <WebMethod()> _
    Public Shared Sub MoveUp(ByVal songId As Integer)
        CurrentPlayListManager.MoveUp(songId)
    End Sub

    <WebMethod()> _
    Public Shared Sub MoveDown(ByVal songId As Integer)
        CurrentPlayListManager.MoveDown(songId)
    End Sub

    <WebMethod()> _
    Public Shared Function SavePlaylist(ByVal PlayListName As String) As String
        MedialibraryManager.SavePlaylist(MySession.currentPlaylistSongs, PlayListName)
        Return "Se guardó la selección"
    End Function

    <WebMethod()> _
    Public Shared Function DeletePlaylist(ByVal PlaylistId As Integer) As String
        MedialibraryManager.DeletePlaylist(PlaylistId)
        Return "Se borró la lista"
    End Function

    Protected Sub btnCancelEditTag_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelEditTag.Click
        RetrocederMenu(1)
    End Sub

    Protected Sub btnCancelEditArtist_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelEditArtist.Click
        RetrocederMenu(1)
    End Sub

    Protected Sub btnCancelEditSong_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelEditSong.Click
        RetrocederMenu(1)
    End Sub

    Protected Sub RetrocederMenu(ByVal cantidad As Integer)

        Dim _text As String = ""
        Dim _commandName As String = ""
        Dim _commandArgument As String = ""

        Breadcrumb.Previous(cantidad, _text, _commandArgument, _commandName)
        ProcesarComando(_text, _commandName, _commandArgument)

    End Sub
End Class